package com.lh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.entity.table.LeaderApproval;
import com.lh.mapper.LeaderApprovalMapper;
import com.lh.service.LeaderApprovalService;
import com.lh.utils.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.extension.toolkit.Db.save;
import static com.baomidou.mybatisplus.extension.toolkit.Db.saveBatch;

@Service
public class LeaderApprovalServiceImpl implements LeaderApprovalService {

    @Autowired
    private LeaderApprovalMapper leaderApprovalMapper;

    private static final List<String> DEPART_LIST = Arrays.asList(
            "黄州区", "团风县", "红安县", "麻城市",
            "罗田县", "英山县", "浠水县", "蕲春县",
            "武穴市", "黄梅县", "黄冈高新区","市临空经济区",
            "龙感湖管理区", "白莲河示范区",
            "市纪委监委机关", "市委组织部", "市委社工部", "市委政法委",
            "市发改委", "市招商服务中心", "市交通运输局", "市资建局",
            "市城管执法委", "市生态环境局", "市水利和湖泊局", "市林业局",
            "市住新局", "市公安局", "市信访局", "市政数局",
            "市应急管理局", "黄冈国投集团", "市经信局", "市教育局", "市科技局",
            "市民政局", "市司法局", "市财政局", "市人力资源和社会保障局", "市农业农村局",
            "市商务局", "市文旅局", "市卫健委", "市政府国资委", "市市场监督局",
            "市机关事务中心", "市公共资源交易中心", "市公共检验检测中心", "市供销社",
            "市体育发展中心", "市国资委"
    );


    @Override
    @Transactional
    public Answer<Object> addLeaderApproval(LeaderApproval leaderApproval) {
        try {
            if (leaderApproval.getApprovedList() != null && !leaderApproval.getApprovedList().isEmpty()) {
                // 批量签批
                List<LeaderApproval> batchList = leaderApproval.getApprovedList().stream().map(department -> {
                    LeaderApproval approval = new LeaderApproval();
                    approval.setRectificationId(leaderApproval.getRectificationId());
                    approval.setRectificationTitle(leaderApproval.getRectificationTitle());
                    approval.setApprover(leaderApproval.getApprover());
                    if (DEPART_LIST.contains(department)){
                        approval.setApprovedDepartment(department);
                    }else{
                        approval.setApprovedPerson(department);
                    }
                    if (StrUtil.isNotBlank(leaderApproval.getApprovedPerson())){
                        approval.setApprovedPerson(leaderApproval.getApprovedPerson());
                    }
                    approval.setContent(leaderApproval.getContent());
                    approval.setSignatureUrl(leaderApproval.getSignatureUrl());
                    approval.setPhotoUrl(leaderApproval.getPhotoUrl());
                    approval.setIsViewed(0);
                    approval.setIsSend(0);
                    approval.setCreatedTime(new Date());
                    return approval;
                }).collect(Collectors.toList());
                // 批量保存
                boolean saveResult = saveBatch(batchList);
                return saveResult ? Answer.ok("批量签批成功") : Answer.error("批量签批失败");
            } else {
                // 单个签批
                leaderApproval.setIsViewed(0);
                leaderApproval.setIsSend(0);
                leaderApproval.setCreatedTime(new Date());
                boolean saveResult = save(leaderApproval);
                return saveResult ? Answer.ok("签批成功") : Answer.error("签批失败");
            }
        } catch (Exception e) {
            return Answer.error("签批异常: " + e.getMessage());
        }
    }

    @Override
    public Answer<Object> getApprovedName() {
        return Answer.ok(DEPART_LIST);
    }

    @Override
    public Answer<Object> readApproval(LeaderApproval leaderApproval) {
        try {
            LeaderApproval approval = leaderApprovalMapper.selectById(leaderApproval.getId());
            if (approval != null) {
                approval.setIsViewed(1);
                approval.setReadTime(new Date());
                int updateResult = leaderApprovalMapper.updateById(approval);
                return updateResult==1 ? Answer.ok("已标记为已读") : Answer.error("标记失败");
            }
            return Answer.error("批示记录不存在");
        } catch (Exception e) {
            return Answer.error("标记异常: " + e.getMessage());
        }
    }

    @Override
    public Answer<Object> queryApprovalPage(JSONObject reqJson) {
        String approvedDepartment = reqJson.getString("approvedDepartment");
        String approver = reqJson.getString("approver");
        String rectificationId = reqJson.getString("rectificationId");
        String rectificationTitle = reqJson.getString("rectificationTitle");
        String approvedPerson = reqJson.getString("approvedPerson");
        Integer isViewed = reqJson.getInteger("isViewed");
        Integer pageSize = reqJson.getInteger("pageSize");
        Integer pageNum = reqJson.getInteger("pageNum");
        Page<LeaderApproval> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LeaderApproval> leaderApprovalLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(approvedDepartment)){
            leaderApprovalLambdaQueryWrapper.eq(LeaderApproval::getApprovedDepartment,approvedDepartment);
        }
        if (StrUtil.isNotBlank(approver)){
            leaderApprovalLambdaQueryWrapper.eq(LeaderApproval::getApprover,approver);
        }
        if (StrUtil.isNotBlank(rectificationId)){
            leaderApprovalLambdaQueryWrapper.eq(LeaderApproval::getRectificationId,rectificationId);
        }
        if (StrUtil.isNotBlank(rectificationTitle)){
            leaderApprovalLambdaQueryWrapper.eq(LeaderApproval::getRectificationTitle,rectificationTitle);
        }
        if (StrUtil.isNotBlank(approvedPerson)){
            leaderApprovalLambdaQueryWrapper.eq(LeaderApproval::getApprovedPerson,approvedPerson);
        }
        if (isViewed != null){
            leaderApprovalLambdaQueryWrapper.eq(LeaderApproval::getIsViewed,isViewed);
        }
        Page<LeaderApproval> approvalPage = leaderApprovalMapper.selectPage(page, leaderApprovalLambdaQueryWrapper);
        return Answer.ok(approvalPage);
    }
}
